D. Prefix Permutation Sums Codeforces Solution